package com.deyuanyun.pic.web.timedtask.tasks.taskservice.impl;

import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.coordinates.CoordinateVO;
import com.deyuanyun.pic.web.mapper.AllPowerfulMapper;
import com.deyuanyun.pic.web.timedtask.TaskConfigUtil;
import com.deyuanyun.pic.web.timedtask.TaskPool;
import com.deyuanyun.pic.web.timedtask.TaskTableInfo;
import com.deyuanyun.pic.web.timedtask.tasks.taskservice.CoordinatesConvertService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by xu on 2016/1/28.
 * 坐标转换的service
 */
@Service
public class CoordinatesConvertServiceImpl implements CoordinatesConvertService {
    static Logger logger=Logger.getLogger(CoordinatesConvertServiceImpl.class);

    @Autowired
    private AllPowerfulMapper allPowerfulMapper;

    @Override
    public void queryZBFromTable(Map map){
        if (!TaskPool.coordTaskTableRecords.isEmpty()){
            return;
        }
        List<TaskTableInfo> t=TaskConfigUtil.getTableList();
        if (TaskPool.coordTaskTable.isEmpty()){
            TaskPool.coordTaskTable.addAll(t);
        }



        if (TaskPool.coordTaskTable.isEmpty()){
            return ;
        }


        for (int i=0;i<t.size();i++){
            TaskTableInfo info= null;
            try {
                info = TaskPool.coordTaskTable.poll(5, TimeUnit.SECONDS);
            } catch (Exception e) {
                logger.error("",e);
                continue;
            }
            if (info==null){
                continue;
            }

            String x="x";
            String y="y";
            String convert_x="convert_x";
            String convert_y="convert_y";
            if (StringUtils.isBlank(info.getTableName())){
                continue;
            }
            if (StringUtils.isNotBlank(info.getX())){
                x=info.getX();
            }
            if (StringUtils.isNotBlank(info.getY())){
                y=info.getY();
            }
            if (StringUtils.isNotBlank(info.getBdx())){
                convert_x=info.getBdx();
            }
            if (StringUtils.isNotBlank(info.getBdy())){
                convert_y=info.getBdy();
            }

            convert_x=convert_x+" ";
            convert_y=convert_y+" ";
            String cols="id_  id,"+x+","+y+",z,"+convert_x+" bdx,"+convert_y+" bdy,'"+info.getTableName()+"' tableName ";
            Map map1=new HashMap();
            map1.put("x",x);
            map1.put("y",y);
            map1.put("convert_x",convert_x);
            map1.put("convert_y",convert_y);
            map1.put("cols",cols);
            map1.put("tableName",info.getTableName());

            try {
                int sss=TaskPool.coordTaskTableRecords.size();
                if (sss>2600){
                    return;
                }
                if (sss<1000){
                    map1.put("recordSize",800);
                }else {
                    map1.put("recordSize",200);
                }
                List<TaskTableInfo> taskTableInfoList = allPowerfulMapper.queryZBFromTable(map1);
                if (ObjectUtil.isEmptyAll(taskTableInfoList)){
                    continue;
                }
                int listNum=taskTableInfoList.size()+sss;
                if (listNum<2600){
                    TaskPool.coordTaskTableRecords.addAll(taskTableInfoList);
                }else {
                    return;
                }
                logger.info("当前表是"+info.getTableName()+","+taskTableInfoList.size());
            } catch (Exception e) {
                logger.error("查询报错!",e);
            }finally {
                try {
                    TaskPool.coordTaskTable.offer(info,5,TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    TaskPool.coordTaskTable.clear();
                    logger.error("",e);
                }
            }

        }
        return ;
    }


    @Override
    public void updateBaiduCoord(CoordinateVO vo){
        if (ObjectUtil.isEmptyAll(vo.getId())||
                ObjectUtil.isEmptyAll(vo.getX())||
                ObjectUtil.isEmptyAll(vo.getY())){
            return;
        }
        Map map=new HashMap();
        map.put("tableName",vo.getTableName());
        map.put("convert_x",vo.getX());
        map.put("convert_y",vo.getY());
        map.put("convert_z",vo.getZ());
        map.put("id",vo.getId());
        allPowerfulMapper.updateBaiduCoord(map);
    }

}
